home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / games / mdlv13.zip / MDLSRC.ZIP / MDL.CC < prev    next >
C/C++ Source or Header  |  1996-07-10  |  11KB  |  403 lines

  1. //
  2. // this file by brian martin 1996
  3. // brian@phyast.pitt.edu
  4. //
  5. // this is part of the source for the MedDLe quake model viewer/editor
  6. //
  7. //
  8. #include "mdl.h"
  9.  
  10. // our good old mdl file data (it's extern)
  11. // I pass the MDL_FILE to most functions, but it isn't implemented yet
  12. // may someday be useful to have multiple mdls open
  13. struct MDL_FILE mdl;
  14. int mdl_keys(MDL_FILE *);
  15.  
  16. // use this stuff for command line options
  17. #define NUM_CFG_OPTIONS 3
  18. char *cfg_option[NUM_CFG_OPTIONS] = {"mode","driver","progspath"};
  19. char driver[128], progspath[128];
  20. unsigned char pal[256*3];
  21. unsigned char cmap[256*32];
  22. //GrContext *BitMapContext;
  23. void OpenColorMap(char *,unsigned char *);
  24.  
  25. //----------------------------------------------------------------------
  26.  
  27. int main (int argc, char *argv[])
  28. {
  29.     FILE *in, *out, *qpal;
  30.     char *in_filename,*out_filename,*filename;
  31.     char str[256];
  32.     char ch;
  33.     short cur_key;
  34.     short draw_3d=1;
  35.     short draw_triangles=0;
  36.     short draw_vertices=0;
  37.     short import_file=0;
  38.     short export_file=0;
  39.     short show_info=0;
  40.     unsigned i,j,k,l;
  41.     unsigned p1,p2,p3,p4;
  42.     int video_mode=0;
  43.     int update=0;
  44.     int refresh=0;
  45.     int oldx,oldy,oldr,oldl,oldm;
  46.     int selected=0;
  47.     int ontopof=0;
  48.     int selectedx, selectedy, selectedindex;
  49.     int madechanges=0;
  50.     int loop;
  51.     int goto_3d=0;
  52.     float zoom_factor=2;
  53.  
  54.     // set default values
  55.     driver[0]=NULL;
  56.     progspath[0]=NULL;
  57.  
  58.     clrscr();
  59.     printf("MedDLe by Brian Martin version 1.3\n\n");
  60.  
  61.     // get config file settings
  62.     if((in=fopen("mdl.cfg","rt"))==NULL)
  63.     {
  64.         fprintf(stderr,"Can't open mdl.cfg, using defaults... \n");
  65.         fprintf(stderr,"Things probably won't work. Hit a key. \n");
  66.         getch();
  67.     }
  68.     else
  69.     {
  70.         fprintf(stderr,"Reading mdl.cfg ...\n");
  71.         ch=0;
  72.         do
  73.         {
  74.             fscanf(in,"%s", str);
  75.             if(feof(in)) break;
  76.             if(str[0]=='#') {while(fgetc(in)!=0x0A);}
  77.             else
  78.             {
  79.                 k=0;
  80.                 for(i=0; i<NUM_CFG_OPTIONS; i++)
  81.                 {
  82.                     j=0;
  83.                     while(*(cfg_option[i]+j)!=NULL)
  84.                     {
  85.                         k=i+1;
  86.                         if(str[j]!=*(cfg_option[i]+j)) {k=0;break;}
  87.                         j++;
  88.                     }
  89.                     if(k!=0) break;
  90.                 }
  91.                 if(k==0) fprintf(stderr,"Don't understand option \"%s\" in config file. \n",str);
  92.                 else
  93.                 {
  94.                     fscanf(in,"%s", str);
  95.                     switch (k)
  96.                     {
  97.                         case 1: video_mode=atoi(str); fprintf(stderr," - setting video mode = %d\n",video_mode); break;
  98.                         case 2: strcpy(driver,str); fprintf(stderr," - setting driver = %s\n",driver);break;
  99.                         case 3: strcpy(progspath,str); fprintf(stderr," - setting progspath = %s\n",progspath);break;
  100.                     }
  101.                 }
  102.             }
  103.         }while(!feof(in));
  104.         fclose(in);
  105.     }
  106.  
  107.  
  108.     // get command line options
  109.     for (argv++, argc--; argc && **argv == '-'; argv++, argc--)
  110.     {
  111.         if ((*argv)[1] == 'e' && argc-- > 1)
  112.         {
  113.             out_filename = *++argv;
  114.             export_file=1;
  115.         }
  116.         else if ((*argv)[1] == 'i' && argc-- > 1)
  117.         {
  118.             in_filename = *++argv;
  119.             import_file=1;
  120.         }
  121.  
  122.         else if ((*argv)[1] == 'm' && argc-- > 1)
  123.             video_mode= atoi( *(++argv));
  124.  
  125.         else if ((*argv)[1] == '2')
  126.             draw_3d = 0;
  127.  
  128.         else if ((*argv)[1] == 's')
  129.             show_info = 1;
  130.  
  131.         else if ((*argv)[1] == 'h')
  132.         {
  133.             fprintf(stderr, "Usage: mdl [-h] [-2] [-s] [-e <file.bmp>] [-i <file.bmp>] [-m #] file.mdl \n");
  134.             fprintf(stderr, "       -h  -- help screen\n");
  135.             fprintf(stderr, "       -2  -- start in 2d view\n");
  136. //            fprintf(stderr, "       -s  -- show header information\n");
  137.             fprintf(stderr, "       -e  -- extract BMP file\n");
  138.             fprintf(stderr, "       -i  -- insert BMP file\n");
  139.             fprintf(stderr, "       -m  -- video mode\n");
  140.             fprintf(stderr, "              0 = 320x200 (default)\n");
  141.             fprintf(stderr, "              1-4 hi res modes\n");
  142.             exit(1);
  143.         }
  144.         else
  145.         {
  146.             fprintf(stderr, "What is %s?  mdl -h will help you", *argv);
  147.             exit(1);
  148.         }
  149.     }
  150.  
  151.     if (argc > 0)
  152.         filename = *argv;
  153.     else
  154.     {
  155.         fprintf(stderr, "mdl -h will help you", *argv);
  156.         exit(1);
  157.     }
  158.  
  159.     // read in mdl file
  160.  
  161.     strcpy(str,progspath);
  162.     strcat(str,filename);
  163.     if((in=fopen(str,"rb"))==NULL)
  164.     {
  165.         printf("can't open mdl file \"%s\" ...exiting\n",str);
  166.         exit(1);
  167.     }
  168.     read_mdl(in, &mdl);
  169.     strcpy(mdl.filename,filename);
  170.     fclose(in);
  171.  
  172.     // setup 3d stuff
  173.     setup_3d_data(&mdl);
  174.  
  175.  
  176.     // read in quake colors
  177.     BG_OpenPalette("mdl.pal",pal);
  178.     //read color map
  179.     OpenColorMap("mdl.map",cmap);
  180.  
  181.     // intiate graphics mode
  182.     BG_GraphicsMode(driver,video_mode);
  183.     // change the screen colors to the quake palette
  184.     BG_SetPalette(pal);
  185.     // initiate mouse
  186.     BG_InitMouse();
  187.     BG_MouseStatus();
  188.     oldx=BG_MouseX;
  189.     oldy=BG_MouseY;
  190.     // intiate math tables
  191.     BG_InitMath();
  192.  
  193.     BG_ClearScreen(0);
  194.     // loop through the 3 modes (3d,2d,help)
  195.     while(1)
  196.     {
  197.         if(draw_3d==1)
  198.         {
  199.             if((i=mdl_3d(&mdl))==0) break;
  200.             if(i==2) draw_3d=2;
  201.             else draw_3d=0;
  202.         }
  203.         if(draw_3d==2)
  204.         {
  205.             if((i=mdl_keys(&mdl))==0) break;
  206.             if(i==1) draw_3d=0;
  207.             else draw_3d=1;
  208.         }
  209.         else
  210.         {
  211.             if((i=mdl_2d(&mdl))==0) break;
  212.             if(i==2) draw_3d=2;
  213.             else draw_3d=1;
  214.         }
  215.  
  216.  
  217.     }
  218.     // go back to text mode
  219.     GrSetModeRestore(0);
  220.     BG_TextMode();
  221.  
  222.     clrscr();
  223.  
  224.  
  225.  
  226.     delete [] mdl.bitmap;
  227.     delete[] mdl.vertex;
  228.     delete [] mdl.triangle;
  229.     for(j=0; j<mdl.num_frames;  j++)
  230.     {
  231.         if(mdl.frame[j].atype!=0) delete [] mdl.frame[j].n3;
  232.         delete [] mdl.frame[j].v;
  233.     }
  234.     delete [] mdl.frame;
  235.     delete_3d_data(&mdl);
  236.  
  237.     fprintf(stderr,"\nMedDLe v1.6 1996\n  by Brian Martin\n  brian@phyast.pitt.edu\n  irc: ZombyWoof\n");
  238.     return(0);
  239. }
  240.  
  241. // read in the color map needed for shading
  242. void OpenColorMap(char *mapfile,unsigned char *cm)
  243. {
  244.     FILE *in;
  245.     in=fopen(mapfile,"rb");
  246.  
  247.     fread(cm,1,256*32,in);
  248.     fclose(in);
  249.  
  250. }
  251. // help file
  252. int mdl_keys(MDL_FILE *m)
  253. {
  254.      int loop=1;
  255.      int draw_3d=2;
  256.      int cur_key;
  257.      static int page=0;
  258.      int MAXPAGE=5;
  259.      int update =1;
  260.      BG_ClearScreen(0);
  261.  
  262.  
  263.         while(loop)
  264.         {
  265.  
  266.             if(kbhit())
  267.             {
  268.                 if((cur_key=getch())==0x0) cur_key=getch();
  269.              //    cur_key=getch();
  270.                 switch(cur_key)
  271.                 {
  272.                 case 27:               loop=0; draw_3d=2; break;
  273.                 case '3':            loop=0; draw_3d=1; break;
  274.                 case '2':            loop=0; draw_3d=0; break;
  275.  
  276.                 case ARROW_RIGHT: case ARROW_DOWN:    if(page<MAXPAGE) page+=1;
  277.                                                         else page=0;
  278.                                                         break;
  279.                 case ARROW_LEFT: case ARROW_UP:        if(page>0) page-=1;
  280.                                                         else page=MAXPAGE;
  281.                                                         break;
  282.  
  283.                 }
  284.                 update=1;
  285.             }
  286.  
  287.          if(update==1)
  288.          {
  289.                  BG_ClearScreen(0);
  290.                 update=0;
  291.  
  292.             if(page==0)
  293.             {
  294.                 // page 0
  295.                 BG_Text("..About MedDLe..",BG_ScreenWidth/2-60,2,250,0);
  296.  
  297.                 BG_Text("MedDLe was written by Brian Martin",5,30,253,0);
  298.                 BG_Text("   (on quake/irc I am the ZombyWoof!)",5,45,253,0);
  299.                 BG_Text("",5,60,253,0);
  300.                 BG_Text("It is free-ware and the source is included.",5,75,253,0);
  301.                 BG_Text("This is NOT an id software product! ",5,90,253,0);
  302.                 BG_Text("Questions or whatever go to:",5,105,253,0);
  303.                 BG_Text("brian@phayst.pitt.edu ",5,120,253,0);
  304.                 BG_Text("http://www.phyast.pitt.edu/~brian",5,135,253,0);
  305.                 BG_Text("",5,150,253,0);
  306.                 BG_Text("I will program for food. (i.e. hire me)",5,155,253,0);
  307.  
  308.                 BG_Text("Use arrow keys (up/down) to see more.",5,170,250,0);
  309.             }
  310.  
  311.             else if(page==1)
  312.             {
  313.                         // page 1
  314.                 BG_Text("..MedDLe Useful Keys..",BG_ScreenWidth/2-70,2,250,0);
  315.                 BG_Text("Key  Action",5,15,250,0);
  316.                 BG_Text("1    Brings up Help",5,30,253,0);
  317.                 BG_Text("2    Brings up (2D) Skin Editor",5,45,253,0);
  318.                 BG_Text("3    Brings up (3D) Frame Editor",5,60,253,0);
  319.                 BG_Text("Esc  Exits",5,75,253,0);
  320.                 BG_Text("These keys always work. For specifics, read on.",5,90,253,0);
  321.  
  322.                 BG_Text("Use arrow keys (up/down) to see more.",5,170,250,0);
  323.             }
  324.             else if(page==2)
  325.             {
  326.                 // page 2
  327.                 BG_Text("..MedDLe Useful Keys..",BG_ScreenWidth/2-70,2,250,0);
  328.                 BG_Text("Keys for 2D Mode",5,15,250,0);
  329.                 BG_Text("1    Brings up Help",5,30,253,0);
  330.                 BG_Text("3    Brings up (3D) Frame Editor",5,45,253,0);
  331.                 BG_Text("w    Show wireframe (triangles)",5,60,253,0);
  332.                 BG_Text("v    Show vertices",5,75,253,0);
  333.                 BG_Text("s    Saves changes",5,90,253,0);
  334.                 BG_Text("Esc  Exits",5,105,253,0);
  335.                 BG_Text("Mouse an